<template>
	<div class="work-station-container">
		<a-row :gutter="[8, 8]">
			<!-- banner -->
			<a-col :span="12" class="banner-box">
				<img class="banner" :src="BannerIcon2" />
			</a-col>
			<!-- 用户卡片 -->
			<a-col :span="12">
				<UserCard :uinfo="userInfoRef" />
			</a-col>
			<!-- 报修动态 -->
			<a-col :span="15">
				<RepairDongTai :dtdata="dongtai" />
			</a-col>
			<!-- 派工趋势图 -->
			<a-col :span="9">
				<RepairQushi :qdata="tognjiDataRef" />
			</a-col>
			<!-- <a-col :span="24"> 
                <RepairMessage/>    
            </a-col> -->
		</a-row>
	</div>
</template>
<script setup>
	import { computed, ref, onMounted, reactive, h } from 'vue';
	import { useRouter } from 'vue-router';
	import { useUserStore } from '@/store/modules/user';
	import tool from '@/utils/tool';
	import { notification } from 'ant-design-vue';
	import { APP_CONFIG } from '@/constants/system-const';

	import RepairDongTai from './repair-dongtai.vue';
	// import RepairMessage from './repair-message.vue';
	import RepairQushi from './repair-qushi.vue';
	import UserCard from './user-card.vue';
	import BannerIcon2 from '@/assets/images/home/banner2.png';
	import { RepairApi } from '@/api/repair-api';

	const userInfoRef = ref({});
	const tognjiDataRef = ref({});
	const dongtai = ref({});

	const initData = async function () {
		tool.showLoading();
		const res = await RepairApi.getUserWorkStation();
		tool.hideLoading();
		if (res.code != '0') {
			tool.error(res.codemessage);
			return;
		}
		let userInfo = {
			userName: res.姓名,
			os_l_time: res.上次登录时间[0].os_l_time,
			departmentName: useUserStore().departmentName,
		};
		userInfoRef.value = userInfo;

		let data06 = getData06(res.data06);
		let tjData = {
			data01: res.data01 && res.data01.length > 0 ? res.data01[0].size_num : 0,
			data02: res.data02 && res.data02.length > 0 ? res.data02[0].size_num : 0,
			data03: res.data03 && res.data03.length > 0 ? res.data03[0].size_num : 0,
			data04: res.data04 && res.data04.length > 0 ? res.data04[0].size_num : 0,
			data05: res.data05 && res.data05.length > 0 ? res.data05[0].size_num : 0,
			data06,
		};
		tognjiDataRef.value = tjData;

		let data07 = {
			data1: res.我发起的,
			data2: res.我处理的,
		};
		dongtai.value = data07;
	};
	const getData06 = function (datas) {
		let dates = getMonths();
		let lastData = [];
		dates.lastYears.forEach((item) => {
			let d = datas.find((val) => {
				return val.rep_wo_insert_time == item;
			});
			if (d) {
				lastData.push(d.size_num);
			} else {
				lastData.push(0);
			}
		});
		let thisData = [];
		var currentTime = new Date();
		let month = currentTime.getMonth();
		month = month + 1;
		dates.thisYears.forEach((item) => {
			let tempMonth = item.split('-')[1] * 1;
			if (month < tempMonth) {
				return;
			}
			let d = datas.find((val) => {
				return val.rep_wo_insert_time == item;
			});
			if (d) {
				thisData.push(d.size_num);
			} else {
				thisData.push(0);
			}
		});
		return {
			lastYear: dates.lastYear,
			thisYear: dates.thisYear,
			lastData,
			thisData,
		};
	};
	const getMonths = function () {
		var currentTime = new Date();
		var previousYear = currentTime.getFullYear() - 1;
		var dates = {
			thisYear: previousYear + 1,
			lastYear: previousYear,
			lastYears: [],
			thisYears: [],
		};
		for (let i = 1; i <= 12; i++) {
			let month = i < 10 ? '0' + i : i;
			dates.lastYears.push(previousYear + '-' + month);
			dates.thisYears.push(previousYear + 1 + '-' + month);
		}
		return dates;
	};

	onMounted(() => {
		initData();
		notification['success']({
			message: '温馨提示',
			description: '您好！欢迎您访问' + APP_CONFIG.websiteName + '！',
			duration: 4,
		});
	});
</script>
<style lang="less" scoped>
	.work-station-container {
		width: 90%;
		min-width: 1000px;
		margin: 0 auto;
		.banner {
			width: 100%;
			height: 300px;
			object-fit: fill;
			border-radius: 4px;
		}
	}
</style>
